import Taro from '@tarojs/taro';

// 生成模拟数据
const generateMockPosts = (count = 10) => {
  const posts = [];
  for (let i = 1; i <= count; i++) {
    posts.push({
      id: i,
      title: `示例帖子标题 ${i}`,
      content: `这是帖子内容，这是第 ${i} 篇帖子的详细内容。内容可以很长，支持显示多行文本。`,
      images: i % 3 === 0 ? [] : [
        'https://img.yzcdn.cn/vant/cat.jpeg',
        'https://img01.yzcdn.cn/vant/apple-1.jpg'
      ],
      video: i % 5 === 0 ? 'https://static.smartisanos.cn/common/video/smartisan-tnt-jianguo.mp4' : '',
      author: {
        id: 1,
        username: 'user' + i,
        nickname: '用户' + i,
        avatar: 'https://img.yzcdn.cn/vant/cat.jpeg',
        bio: '这是用户简介',
        createdAt: new Date().toISOString()
      },
      createdAt: new Date(Date.now() - i * 3600000).toISOString(),
      commentCount: Math.floor(Math.random() * 100),
      likeCount: Math.floor(Math.random() * 200),
      isLiked: Math.random() > 0.5
    });
  }
  return posts;
};

// 初始化本地存储中的帖子数据
const initPostsData = () => {
  let posts = Taro.getStorageSync('posts');
  if (!posts) {
    posts = generateMockPosts(20);
    Taro.setStorageSync('posts', posts);
  }
  return posts;
};

// 获取帖子列表
export const getPosts = (params: { page: number; pageSize: number }) => {
  const posts = initPostsData();
  const { page, pageSize } = params;
  const start = (page - 1) * pageSize;
  const end = page * pageSize;
  const pageData = posts.slice(start, end);
  
  return Promise.resolve({
    list: pageData,
    total: posts.length,
    page,
    pageSize,
    hasMore: end < posts.length
  });
};

// 获取帖子详情
export const getPostDetail = (id: number) => {
  const posts = initPostsData();
  const post = posts.find(item => item.id === id);
  
  if (!post) {
    return Promise.reject(new Error('帖子不存在'));
  }
  
  return Promise.resolve(post);
};

// 创建帖子
export const createPost = (params: any) => {
  const posts = initPostsData();
  const userInfo = Taro.getStorageSync('userInfo');
  
  if (!userInfo) {
    return Promise.reject(new Error('请先登录'));
  }
  
  const newPost = {
    id: posts.length + 1,
    ...params,
    author: {
      id: userInfo.id,
      username: userInfo.username,
      nickname: userInfo.nickname,
      avatar: userInfo.avatar,
      bio: userInfo.bio,
      createdAt: userInfo.createdAt
    },
    createdAt: new Date().toISOString(),
    commentCount: 0,
    likeCount: 0,
    isLiked: false
  };
  
  posts.unshift(newPost);
  Taro.setStorageSync('posts', posts);
  
  return Promise.resolve(newPost);
};

// 点赞帖子
export const likePost = (id: number) => {
  const posts = initPostsData();
  const postIndex = posts.findIndex(item => item.id === id);
  
  if (postIndex === -1) {
    return Promise.reject(new Error('帖子不存在'));
  }
  
  posts[postIndex].isLiked = true;
  posts[postIndex].likeCount += 1;
  
  Taro.setStorageSync('posts', posts);
  
  return Promise.resolve({ success: true });
};

// 取消点赞
export const unlikePost = (id: number) => {
  const posts = initPostsData();
  const postIndex = posts.findIndex(item => item.id === id);
  
  if (postIndex === -1) {
    return Promise.reject(new Error('帖子不存在'));
  }
  
  posts[postIndex].isLiked = false;
  posts[postIndex].likeCount = Math.max(0, posts[postIndex].likeCount - 1);
  
  Taro.setStorageSync('posts', posts);
  
  return Promise.resolve({ success: true });
};

// 上传图片 (模拟)
export const uploadImage = (filePath: string) => {
  // 模拟上传延迟
  return new Promise<string>((resolve) => {
    setTimeout(() => {
      // 返回网络图片链接
      const images = [
        'https://img.yzcdn.cn/vant/cat.jpeg',
        'https://img01.yzcdn.cn/vant/apple-1.jpg',
        'https://img01.yzcdn.cn/vant/sand.jpg',
        'https://img01.yzcdn.cn/vant/tree.jpg'
      ];
      const randomImage = images[Math.floor(Math.random() * images.length)];
      resolve(randomImage);
    }, 500);
  });
};

// 上传视频 (模拟)
export const uploadVideo = (filePath: string) => {
  // 模拟上传延迟
  return new Promise<string>((resolve) => {
    setTimeout(() => {
      // 返回网络视频链接
      resolve('https://static.smartisanos.cn/common/video/smartisan-tnt-jianguo.mp4');
    }, 1000);
  });
}; 